﻿using BaseDll;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using UIDATALayer;
namespace UserData
{
    public enum BoxStatus
    {
        屏蔽中,
        空炉,
        空炉待进料,
        满炉已进料,
        预热过程,
        固化中,
        满炉待取走,
    }
    [Serializable]
    public class OvenDataInfo
    {
        public int n_TrayCount = 1;
        public string str_Tray1Code = "";
        public string str_Tray2Code = "";
        public List<string> List_SN_Tray1 = new List<string>();
        public List<string> List_SN_Tray2 = new List<string>();
        public int n_OvenIndex = 0;
        public double d_烤箱加热开始时温度 = 0;
        public double d_烤箱加热结束时温度 = 0;
        public DateTime time_烤箱加热开始时间 = DateTime.Now;
        public DateTime time_烤箱加热结束时间 = DateTime.Now;

        public OvenDataInfo Get_CloneObject()
        {
            using (Stream objectStream = new MemoryStream())
            {
                IFormatter formatter = new BinaryFormatter();
                formatter.Serialize(objectStream, this);
                objectStream.Seek(0, SeekOrigin.Begin);
                return (OvenDataInfo)formatter.Deserialize(objectStream);
            }

        }
    }
    public class OvenData
    {
        public DateTime date_BeginTime = DateTime.MinValue;
        public BoxStatus _Status = BoxStatus.空炉;
        public OvenDataInfo snInfo = new OvenDataInfo();
    }
    public class BoxStatueData : ShowData
    {
        public int nBox { get; set; }
        public BoxStatus boxStatus { get; set; }
    }
    public class BoxTimeReflushData : ShowData
    {
        public int nBox { get; set; }
        public DateTime time { get; set; }
    }
    public class OvenStatueData : ShowData
    {
        public int nBox { get; set; }
        public bool b_IsConnect { get; set; }
        public bool b_IsOpen { get; set; }
        public double d_ActTemp { get; set; }
        public double d_SetTemp { get; set; }
        public string str_Mode { get; set; }
    }
    public class OvenMgr
    {
        public delegate void EventSetOverStartTimeDelegate(int nBox, DateTime date);
        public event EventSetOverStartTimeDelegate EventSetOverStartTime = null;
        public delegate void EventSetOverStatueDelegate(int nBox,  BoxStatus status);
        public event EventSetOverStatueDelegate EventSetOvenState = null;
        public Dictionary<int, OvenData> dic_OvenData = new Dictionary<int, OvenData>();
        string str_PathName = @"D:\HY System\_Machine\Config.炉子数据.json";
        private static OvenMgr ovenMgr = null;
        private static object obj = new object();
        private static object obj_Save = new object();
        private OvenMgr()
        {
            dic_OvenData.Clear();
            for (int i = 1; i < 7; i++)
            {
                dic_OvenData.Add(i, new OvenData());
            }
        }
        public static OvenMgr GetInstance()
        {
            if (ovenMgr == null)
            {
                lock (obj)
                {
                    ovenMgr = new OvenMgr();
                    return ovenMgr;
                }

            }
            else
                return ovenMgr;
        }
        public void Init()
        {
            if (!File.Exists(str_PathName))
            {
                AccessJosnSerializer.ObjectToJson(str_PathName, dic_OvenData);
            }
            else
            {
                object obj = AccessJosnSerializer.JsonToObject(str_PathName, typeof(Dictionary<int, OvenData>));
                if (obj != null)
                {
                    dic_OvenData = (Dictionary<int, OvenData>)obj;
                }
            }
            foreach (var item in dic_OvenData)
            {
                if (EventSetOverStartTime != null)
                    EventSetOverStartTime(item.Key,item.Value.date_BeginTime);
                if (EventSetOvenState != null)
                    EventSetOvenState(item.Key, item.Value._Status);
            }

        }
        private void Function_异步保存数据()
        {
            AccessJosnSerializer.ObjectToJson(str_PathName, dic_OvenData);

        }

        public void Function_记录炉子加热开始时间(int n_Index,DateTime dateTime)
        {
            if(!dic_OvenData.ContainsKey(n_Index))
            {
                dic_OvenData.Add(n_Index, new OvenData());
            }
            dic_OvenData[n_Index].date_BeginTime = dateTime;
            Function_异步保存数据();
            if (EventSetOverStartTime != null)
                EventSetOverStartTime(n_Index, dateTime);
        }
        public void Function_记录炉子当前状态(int n_Index, BoxStatus statue)
        {
            if (!dic_OvenData.ContainsKey(n_Index))
            {
                dic_OvenData.Add(n_Index, new OvenData());
            }
            dic_OvenData[n_Index]._Status = statue;
            Function_异步保存数据();
            if (EventSetOvenState != null)
                EventSetOvenState(n_Index, statue);
        }
        public BoxStatus Function_获取炉子当前状态(int n_Index)
        {
            if (dic_OvenData.ContainsKey(n_Index))
            {
                return dic_OvenData[n_Index]._Status;
            }
            else
            {
                return BoxStatus.屏蔽中;
            }

        }
        public DateTime Function_获取炉子加热开始时间(int n_Index)
        {
            if (dic_OvenData.ContainsKey(n_Index))
            {
                return dic_OvenData[n_Index].date_BeginTime;
            }
            else
            {
                return DateTime.MinValue;
            }

        }

        public void Function_清除烤炉数据(int n_Index)
        {
            if (dic_OvenData.ContainsKey(n_Index))
            {
                dic_OvenData[n_Index] = new OvenData();
            }
            Function_异步保存数据();
            if (EventSetOverStartTime != null)
                EventSetOverStartTime(n_Index, DateTime.MinValue);
            if (EventSetOvenState != null)
                EventSetOvenState(n_Index, BoxStatus.空炉);

        }
    }
}